import com.facebook.Facebook;
import com.facebook.commands.photos.GetAlbums;
import com.facebook.commands.photos.GetPhotos;
import com.facebook.commands.stream.PublishPost;
import com.facebook.commands.users.HasAppPermission;
import com.facebook.data.BooleanResultData;
import com.facebook.data.auth.ExtendedPermissionValues;
import com.facebook.data.feed.ActionLinkData;
import com.facebook.data.photos.GetAlbumsData;
import com.facebook.data.photos.GetPhotosData;
import com.facebook.data.users.HasAppPermissionValues;
import com.facebook.events.FacebookEvent;
import com.facebook.net.FacebookCall;
import com.facebook.utils.FacebookSessionUtil;

import mx.collections.ArrayCollection;
			
	private var fbook:Facebook;
	private var session:FacebookSessionUtil;
			
	private var API_KEY:String    = "7b31fb40987dc545ab6d267ca1bdd4d9";
	
	private var SECRET_KEY:String = "d56d63aab276ad13f5aea0488358fe18"; 
	
	private var _hasStreamsPermission:Boolean = false;
			
	[Bindable] private var facebookPhotoAlbums:ArrayCollection; 
	[Bindable] private var facebookPhotos:ArrayCollection;
	[Bindable] private var facebookEvents:ArrayCollection;
			
	private function initFacebookSession():void {
		session = new FacebookSessionUtil(API_KEY, SECRET_KEY, loaderInfo);
		fbook = new Facebook();
		fbook.addEventListener(FacebookEvent.CONNECT, onConnect, false, 0, true);
		fbook.startSession(session.activeSession);
	}
			
	private function onConnect(e:FacebookEvent):void {	
		fbook.refreshSession();
	}
			
	private function doneLoggingIn():void {
		session.validateLogin();
		var call:FacebookCall = fbook.post(new HasAppPermission(HasAppPermissionValues.PUBLISH_STREAM));
		call.addEventListener(FacebookEvent.COMPLETE, handlePermissionsResult);
		fbook.grantExtendedPermission(ExtendedPermissionValues.PHOTO_UPLOAD);
	}
		
	private function getPhotoAlbums():void {
		var call:FacebookCall = fbook.post(new GetAlbums(fbook.uid));
		call.addEventListener(FacebookEvent.COMPLETE, handleGetAlbumsResponse);
	}
		
	private function handleGetAlbumsResponse(e:FacebookEvent):void {
		var albumsResponseData:GetAlbumsData = e.data as GetAlbumsData;
				
		if (!albumsResponseData || e.error){ 
			/**
			 *  an error occurred
			 **/
			return;
		}
		facebookPhotoAlbums = new ArrayCollection();							
		Alert.show('I found ' + albumsResponseData.albumCollection.length + ' albums');									
		for(var i:int = 0; i < albumsResponseData.albumCollection.length; i++){
			facebookPhotoAlbums.addItem(albumsResponseData.albumCollection.getItemAt(i));	
		}
		bottomPanelWrapper.albumsDataGrid.dataProvider = facebookPhotoAlbums;
	}
			
	private function handleGetPhotosResponse(e:FacebookEvent):void {
		var photosResponseData:GetPhotosData = e.data as GetPhotosData;
				
		if (!photosResponseData || e.error){
			/**
			 *  an error occurred
			 **/
			return;
		}				
		facebookPhotos = new ArrayCollection();										
		for(var i:int = 0; i < photosResponseData.photoCollection.length; i++){
			facebookPhotos.addItem(photosResponseData.photoCollection.getItemAt(i));	
		}
		bottomPanelWrapper.photosList.dataProvider = facebookPhotos;
	}
	
	private function handlePermissionsResult(e:FacebookEvent):void
	{
		_hasStreamsPermission = e.data as BooleanResultData;
	}
	
	private function handleWallPostComplete(e:FacebookEvent):void
	{
		if (e.error) {
			 // an error occurred
			return;
		}
		
		Alert.show('Note posted!');
	}
			
	private function showSelectedAlbum():void {
		var selectedAlbumID:String = bottomPanelWrapper.albumsDataGrid.selectedItem.aid;						
		var call:FacebookCall = fbook.post(new GetPhotos('', selectedAlbumID));
		call.addEventListener(FacebookEvent.COMPLETE, handleGetPhotosResponse);
	}
	
	private function postOnWall(text:String)
	{
		if (_hasStreamsPermission == false) {
			fbook.grantExtendedPermission(ExtendedPermissionValues.PUBLISH_STREAM);
		}
		var actionLinkData:ActionLinkData = new ActionLinkData();
		actionLinkData.href = "http://www.adobe.com/products/flex/";
		actionLinkData.text = "Created using Flex";
		var call:FacebookCall = fbook.post(new PublishPost(text, null, [actionLinkData]));
		call.addEventListener(FacebookEvent.COMPLETE, handleWallPostComplete);
	}